package com.taomeishu.mailcrazy;

import java.io.IOException;

import com.taomeishu.mailcrazy.http.ClientConnector;
import com.taomeishu.mailcrazy.http.ClientResponse;
import com.taomeishu.mailcrazy.http.JDKClientConnector;
import com.taomeishu.mailcrazy.parser.ResultParseException;
import com.taomeishu.mailcrazy.parser.ResultParser;

public abstract class AbstractMailExistChecker implements MailExistChecker {

	private ClientConnector clientConnector = new JDKClientConnector();
	
	@Override
	public CheckResult checkMail(String username, String domainName) throws IOException, ResultParseException {
		String url = makeRequestUrl(username, domainName);
		try {
			ClientResponse response = getClientConnector().request(url, usePostMethod(), 
					getPostData(username, domainName), getReferer(), 
					getCookies(username, domainName), getContentType());
			return getResultParser().parse(response, username, domainName);
		} catch (IOException e) {
			throw new IOException("hanlde url "+ url + " error", e);
		}
	}
	
	protected abstract String makeRequestUrl(String username, String domainName);
	
	protected abstract ResultParser getResultParser();
	
	protected String getContentType() {
		return null;
	}
	
	protected String getReferer() {
		return null;
	}
	
	protected boolean usePostMethod() {
		return false;
	}
	
	protected byte[] getPostData(String username, String domainName) {
		return null;
	}
	
	protected String getCookies(String username, String domainName) {
		return null;
	}

	protected ClientConnector getClientConnector() {
		return clientConnector;
	}
	
}
